package com.yinhe.music.yhmusic.base;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.apkfuns.logutils.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.listener.OnPlayerEventListener;
import com.yinhe.music.yhmusic.login.LoginRegisterActivity;
import com.yinhe.music.yhmusic.utils.StatusBarUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author 何晓华
 */
public abstract class BaseServiceActivity extends BaseActivity implements IBaseView {
    public Unbinder unbinder;

    @BindView(R.id.loading_progress)
    ProgressBar progressBar;

    public int mPage = 1;
    public int mPageSize = 10;

    /**
     * 初始化界面
     */
    public abstract void initView();

    /**
     * 设置布局
     *
     * @return layout id
     */
    public abstract int getLayoutResId();

    /**
     * 获取Presenter实例，子类实现
     *
     * @return
     */
    public abstract BasePresenter getPresenter();

    /**
     * 初始化Presenter的实例，子类实现
     */
    public abstract void initPresenter();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        initPresenter();
        super.onCreate(savedInstanceState);
        bindView();
    }

    @Override
    protected void onDestroy() {
        if (mService != null) {
            mService.removePlayEventListner(this);
        }

        if (getPresenter() != null) {
            getPresenter().onDetach();
            getPresenter().dispose();
        }
        unBindService();
        super.onDestroy();
    }

    public void bindView() {
        StatusBarUtil.setStatusBarTranslucent(this, true);
        setContentView(getLayoutResId());
        unbinder = ButterKnife.bind(this);
        initView();
    }

    @Override
    public void setContentView(int layoutResID) {
        View view = getLayoutInflater().inflate(R.layout.activity_default_view, null);
        //设置填充activity_base布局
        super.setContentView(view);

        //加载子类Activity的布局
        initDefaultView(layoutResID);
    }

    /**
     * 设置默认布局
     *
     * @param layoutResId 布局
     */
    private void initDefaultView(int layoutResId) {
        FrameLayout container = findViewById(R.id.fl_activity_child_container);
        View childView = LayoutInflater.from(this).inflate(layoutResId, null);
        container.addView(childView, 0);
    }


    @Override
    public void showLoading() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showMessage(Throwable throwable) {
        Toast.makeText(this, throwable.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showPlayList() {
        playQueueFragment.show(getSupportFragmentManager(), "playlistframent");
    }

    @Override
    public Context getContext() {
        return BaseServiceActivity.this;
    }

    @Override
    public void startLogin() {
        startActivity(new Intent(this, LoginRegisterActivity.class));
    }

    @Override
    public void setLoadMoreList(List list, int pageNum, BaseQuickAdapter mAdapter) {
        int size = list.size();

        if (mPage == 1) {
            mAdapter.setNewData(list);
        }
        if (mPage > 1 && size > 0) {
            mAdapter.addData(list);
        }

        if (mPage >= pageNum) {
            mAdapter.loadMoreEnd();
        } else {
            mAdapter.loadMoreComplete();
            mPage++;
        }
    }

    public void hideToolbar() {
        LinearLayout mToolbarLayout = findViewById(R.id.app_bar);
        mToolbarLayout.removeAllViews();
    }

    /**
     * 设置透明toolbar
     *
     * @param text 标题
     */
    protected void setTransparentToolbar(String text) {
        Toolbar transparentToolbar = findViewById(R.id.transparent_toolbar);

        if (transparentToolbar != null) {
            setSupportActionBar(transparentToolbar);
            TextView title = findViewById(R.id.transparent_toolbar_title);
            title.setText(text);
            transparentToolbar.setTitle("");
        }

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
            actionBar.setTitle("");
        }
    }
}
